package com.scap.authen.service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.scap.authen.dao.RoleDao;
import com.scap.authen.entity.Role;
import com.scap.authen.entity.Site;
import com.scap.authen.util.JDate;

@Service ("roleService")
@Transactional
public class RoleServiceImpl implements RoleService{

	@Autowired
	private RoleDao voRoleDao;
	@Autowired
	private ApplicationContext voContext;

	@Override
	public List<Object> findAllRole() {
		return voRoleDao.getAllRole();
	}
	
	@Override
	public List<Object> getListBasedOnSearchParameter(String searchParameter,List<Object> roleList) {
		
		if (null != searchParameter && !searchParameter.equals("")) {
			List<Object> roleForSearch = new ArrayList<Object>();
			searchParameter = searchParameter.toUpperCase();
			for (int i=0; i<roleList.size(); i++) {
				
				Role role = (Role) roleList.get(i);
				if (role.getVaRoleId().toUpperCase().indexOf(searchParameter)!= -1 || role.getVaRoleName().toUpperCase().indexOf(searchParameter)!= -1
						|| role.getVaStatus().toUpperCase().indexOf(searchParameter)!= -1 || role.getVaAppStatus().toUpperCase().indexOf(searchParameter)!= -1
						|| role.getVaMenuStatus().toUpperCase().indexOf(searchParameter)!= -1) {
					roleForSearch.add(role);					
				}
				
			}
			roleList = roleForSearch;
			roleForSearch = null;
		}
		return roleList;
	}

	@Override
	public String saveRole(Role poRole, String paSiteId, HttpServletRequest request) {
		String vaResult = "";
		Locale voLocale = RequestContextUtils.getLocale(request);
		poRole.setVaAppStatus("N");
		poRole.setVaMenuStatus("N");
		poRole.setVaSiteId(paSiteId);
		poRole.setVnInsertUserId(1);
		poRole.setVaInsertDate(JDate.getDate());
		poRole.setVaInsertTime(JDate.getTime());
		
		if ( voRoleDao.insertRole(poRole)){
			vaResult = voContext.getMessage("alert.save.success", null, voLocale);
		} else {
			vaResult = voContext.getMessage("alert.save.fail", null, voLocale);
		}
		
		return vaResult;
	}

	@Override
	public String updateSite(Role poRole, String paSiteId, HttpServletRequest request) {
		String vaResult = "";		
		Locale voLocale = RequestContextUtils.getLocale(request);	
		poRole.setVaSiteId(paSiteId);
		poRole.setVnUpdateUserId(1);
		poRole.setVaUpdateDate(JDate.getDate());
		poRole.setVaUpdateTime(JDate.getTime());
		
		if ( voRoleDao.updateSite(poRole)){
			vaResult = voContext.getMessage("alert.save.success", null, voLocale);
		} else {
			vaResult = voContext.getMessage("alert.save.fail", null, voLocale);
		}	
		return vaResult;
	}

	@Override
	public void updateAppStatus(String vaRoleId, String vaSiteId) {
		voRoleDao.updateAppStatus(vaRoleId, vaSiteId);
	}
	
	@Override
	public void updateMenuStatus(String vaRoleId, String vaSiteId) {
		voRoleDao.updateMenuStatus(vaRoleId, vaSiteId);
	}

	@Override
	public Map<String, String> getRoleList() {
		Map<String,String> voRole = new LinkedHashMap<String,String>();
		
		List<Object[]> voRoleList = voRoleDao.getRoleList();
		
		for(Object[] voObject : voRoleList){

			voRole.put(String.valueOf(voObject[0]), String.valueOf(voObject[1]));

		}
		return voRole;
	}	
}
